package org.tp8.sampleapp.loader;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.tp8.sampleapp.loader.exc.SampleLoaderException;
import org.tp8.sampleapp.loader.util.SampleLoaderLogger;

/**
 * The Class MTMain contains the main method that start the batch service
 * execution by passing the needed parameters. Then the executor thread starts,
 * which adds and updates the batch with the running jobs. It also generates a
 * logger to keep track of all the precess's steps
 * 
 */
public class Main {

	/** An application context. */
	private static ClassPathXmlApplicationContext appContext;

	/** The log. */
	private static SampleLoaderLogger log;

	/**
	 * The main method first initialize spring, acquire an instance of the batch
	 * service creates a new instance of the logger, and starts the thread with
	 * the necessary arguments from the command line The thread checks and
	 * flushes the batch and before it ends, it checks it one last time The
	 * duration of the whole process is being calculated and the process ends
	 * 
	 * @param args
	 *            the arguments
	 */
	public static void main(String[] args) {

		// Initialize Spring
		appContext = new ClassPathXmlApplicationContext(
				"classpath:spring/applicationContext-main.xml");

		log = new SampleLoaderLogger(Main.class);

		long startTime = System.currentTimeMillis();

		Executor executor = appContext.getBean(Executor.class);

		try {
			executor.run(args);
		} catch (SampleLoaderException e) {
			log.error(e);
		}

		long duration = System.currentTimeMillis() - startTime;

		log.info("Duration (secs): " + (duration / 1000));

		appContext.destroy();

		System.exit(0);
	}

}
